﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.RegInfo.Models;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class AdminActionBL
    {
        public List<AdminActionExtend> GetAll()
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AdminActionExtend>(Common.Constants.StoredProcedures.GetAllAdminActions, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateAdminAction(AdminAction action)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@Code", action.Code);
                adapter.AddParameter("@Name", action.Name);
                adapter.AddParameter("@ActionGroupID", action.ActionGroupID);
                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminAction, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminAction(AdminAction action)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@ActionID", action.ActionID);
                adapter.AddParameter("@Code", action.Code);
                adapter.AddParameter("@Name", action.Name);
                adapter.AddParameter("@ActionGroupID", action.ActionGroupID);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateAdminAction, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminActionExtend GetByActionCode(string code)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Code", code);
            var list = adapter.GetList<AdminActionExtend>(Common.Constants.StoredProcedures.GetAdminActionByCode, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public AdminActionExtend GetByActionID(int id)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@ActionID", id);
            var list = adapter.GetList<AdminActionExtend>(Common.Constants.StoredProcedures.GetAdminActionByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteAdminActionByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM AdminActions WHERE ActionID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminActionByActionGroupID
        public List<AdminAction> GetAdminActionsByUserGroupID(int usergroupid)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserGroupID", usergroupid);
            var list = adapter.GetList<AdminAction>(Common.Constants.StoredProcedures.GetAdminActionByUserGroupID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        //sp_AdminActions_Select_By_UserID
        public List<AdminAction> GetAdminActionsByUserID(int userid)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserID", userid);
            var list = adapter.GetList<AdminAction>(Common.Constants.StoredProcedures.GetAdminActionByUserID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        //  sp_AdminActions_Select_By_UserName
        public List<AdminAction> GetAdminActionsByUserName(string username)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserName", username);
            var list = adapter.GetList<AdminAction>(Common.Constants.StoredProcedures.GetAdminActionByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
    }
}
